/**
 * 郭楠project
 * com.gn.controller
 * CommentController.java
 * 创建人:TonyGuo 
 * 时间：2016年12月14日-下午9:57:19 
 * 2016Tm公司-版权所有
 */
package com.gn.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gn.dto.UserCommentDTO;
import com.gn.entity.AdminUser;
import com.gn.entity.Comment;
import com.gn.entity.CommentZanPaizhuan;
import com.gn.entity.R;
import com.gn.service.CommentService;
import com.gn.service.CommentZanPaizhuanService;
import com.gn.service.GoodsService;
import com.gn.util.CommonUtils;
import com.mysql.jdbc.StringUtils;
import com.taoxeo.commons.lang.BeanUtils;

/**
 * 
 * CommentController
 * 创建人:TonyGuo
 * 时间：2016年12月14日-下午9:57:19 
 * @version 1.0.0
 * 
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
	Logger logger = LoggerFactory.getLogger(CommentController.class);
	@Resource
	private CommentService commentService;
	
	@Resource
	private CommentZanPaizhuanService commentZanPaizhuanService;
	
	@RequestMapping(value="/all")
	public List<UserCommentDTO> findCommentList(HttpServletRequest req,
			@RequestParam(value="gid" ,required =true ) String gid,
			@RequestParam(value="page" ,required =false,defaultValue="0" ) Integer page){
		Map<String, Object> mapPage = new HashMap<>();
		Integer limit = 10;//每页显示10条记录
		//page=1;
		mapPage.put("offset", page * limit);		
		mapPage.put("limit", limit);
		mapPage.put("gid", gid);
		//req.getSession().setAttribute("comment_page", page);
		
		List<Comment> list=commentService.findAllUserCommentsById(mapPage);
		List<UserCommentDTO> listCommentZanPaizhuan=new ArrayList<>();
		AdminUser user=(AdminUser)req.getSession().getAttribute("curr_user");
		for (Comment comment : list) {
			UserCommentDTO userCommentDTO=new UserCommentDTO();
			org.springframework.beans.BeanUtils.copyProperties(comment, userCommentDTO);
			
			Map<String,Object> map=new HashMap<>();
			map.put("uid", user.getUserid());
			map.put("cid", comment.getId());
			List<CommentZanPaizhuan> czps=commentZanPaizhuanService.findUserCommentZanOrPaizhuan(map);
			if(commentZanPaizhuanService.findUserCommentZanOrPaizhuan(map)!=null){
				logger.info("【===>:{}】",czps);
				for (CommentZanPaizhuan commentZanPaizhuan : czps) {
					if("0".equals(commentZanPaizhuan.getStatus())){
						userCommentDTO.setZanType(commentZanPaizhuan.getStatus());
					}
					if("1".equals(commentZanPaizhuan.getStatus())){
						userCommentDTO.setZhuanType(commentZanPaizhuan.getStatus());
					}
				}
				
			}
			listCommentZanPaizhuan.add(userCommentDTO);
		}
		return listCommentZanPaizhuan;
	}
	
	@RequestMapping(value="/save")
	public R saveComment(HttpServletRequest req, Comment comment){
		AdminUser user=(AdminUser)req.getSession().getAttribute("curr_user");
		if(user.getUsernick()==null || "".equals(user.getUsernick())){
			try {
				comment.setUserName(CommonUtils.Iso2UTF8(user.getUsername()));
				comment.setUserImg(user.getUserPic());
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}else{
			comment.setUserName(user.getUsernick());
		}
		if(user.getUserPic()!=null && !"".equals(user.getUserPic())){
			comment.setUserImg(user.getUserPic());
		}
		comment.setUid(user.getUserid());
		commentService.addUserComment(comment);
		return R.ok();
	}
	
	/**
	 * 【点赞】
	 * @param zanCount 点赞数
	 * @param id 该评论的id
	 */
	@RequestMapping(value="/dianzan")
	public boolean addZanCount(String id) {
		return commentService.addZanCount(id);
	}

	/**
	 * 【拍砖】
	 * @param paizhuanCount 拍砖数
	 * @param id 该评论的id
	 */
	@RequestMapping(value="/paizhuan")
	public boolean addPaiZhuanCount(String id) {
		return commentService.addPaiZhuanCount(id);
	}

}
